<?
// Search Resources

// Get total number of matching records
$sqlSR = "SELECT LinkID,MATCH (Title,LinkDescription,Link) AGAINST
		('$q') AS searchScore
		FROM transLinks WHERE (MATCH (Title,LinkDescription,Link) AGAINST ('$q') OR
		( Title LIKE '%$q%' OR LinkDescription LIKE '%$q%' OR Link LIKE '%$q%' )) AND
		Status = 1
		GROUP BY Title
		ORDER BY searchScore";
$resultSR = mysql_db_query($dbname,$sqlSR);
$resourceRows = mysql_num_rows($resultSR);
$totalResults = $resourceRows;
// Determine number of pages
if ($resourceRows > 20) { $resourcePages = ceil($resourceRows/20); }
if (empty($start)) { $start = 0; }
if ($resourceRows > 0) { 
	$results = 1;
	$resultsResources = 1;
	
	// Get actual results
	$sqlSR = "SELECT LinkID,Path,Title,LinkDescription,Link,DateCreated,Views,MATCH (Title,LinkDescription,Link) AGAINST
			('$q') AS searchScore
			FROM transLinks WHERE ( ( MATCH (Title,LinkDescription,Link) AGAINST ('$q') ) OR
			( Title LIKE '%$q%' OR LinkDescription LIKE '%$q%' OR Link LIKE '%$q%' ) ) AND 
			Status = '1'
			GROUP BY Title
			ORDER BY searchScore 
			DESC LIMIT $start,20";
	$resultSR = mysql_db_query($dbname,$sqlSR);
	$rows = mysql_num_rows($resultSR);
	
	$i = 0;
	// Put everything into arrays
	while ($valueSR = mysql_fetch_assoc($resultSR)) {
		// Check title against existing titles so we can eliminate duplicates
		// (there are several dup entries with different paths)	
		$linkIDArray[$i] = $valueSR[LinkID];
		$pathArray[$i]   = $valueSR[Path];
		$titleArray[$i]  = DBToField($valueSR[Title]);
		$descArray[$i]   = DBToField($valueSR[LinkDescription]);
		$linkArray[$i]   = $valueSR[Link];
		$dateArray[$i]   = $valueSR[DateCreated];
		$viewsArray[$i]  = $valueSR[Views];
		
		$relevance = round($valueSR[searchScore],3);
		
		if ( ($i == 0) && (empty($_SESSION[searchRel])) ) { $_SESSION[searchRel] = $relevance; }
		if ( $_SESSION[searchRel] > 0 ) {
			$relPerArray[$i] = round( ($relevance/$_SESSION[searchRel]),1 )*100;		
		}
		
		$i++;
	}
	$results = $results+$resourceRows;
}
?>